﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.ArcGIS;
using System.IO;
using ESRI.ArcGIS.Display;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using DevComponents.Editors;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 符号样式处理类
    /// </summary>
    public partial class StyleColor : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public StyleColor()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StyleColorContext_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                Create();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private Color _SelectedColor = Color.White;

        /// <summary>
        /// 
        /// </summary>
        public Color SelectedColor
        {
            get { return _SelectedColor; }
            set
            {
                _SelectedColor = value;
                SetSelected(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public event ColorSelectedEvent SelectChanged = null;

        /// <summary>
        /// 显示控件
        /// </summary>
        public void Init()
        {
            sptbColor.SelectedTab = tbitmStyle;
        }

        /// <summary>
        /// 初始化颜色颜色
        /// </summary>
        private void Create()
        {
            if (string.IsNullOrWhiteSpace(AoHelper.StylePath)) return;
            if (itmpnlColor.Items.Count > 0) return;            
            string f = string.Format("{0}\\{1}", AoHelper.StylePath, "ESRI.style");
            if (!File.Exists(f)) return;
            List<SymbolItem> styles = SymbolHelper.GetStyles(zjgisStyleGalleryType.Colors, f);
            if (styles == null || styles.Count == 0) return;
            Color nocolor = Color.FromName("ffedeada");
            btnNoColor.Tag = nocolor;
            btnNoColor.Text = GetName(nocolor);
            btnNoColor.Name = GetName(nocolor);
            foreach (SymbolItem itm in styles)
            {
                IColor color = (IColor)itm.Symbol;
                Color tmpcolor = Convertor.IColor2Color(color);
                ButtonItem btnColor = new ButtonItem();
                btnColor.Command = cmdColor;
                btnColor.Text = GetName(tmpcolor);
                btnColor.Name = GetName(tmpcolor);
                btnColor.Tag = tmpcolor;
                btnColor.Tooltip = itm.Name;
                btnColor.Symbol = "";
                btnColor.SymbolColor = tmpcolor;
                btnColor.OptionGroup = "Color";
                btnColor.FixedSize = new Size(32, 32);
                btnColor.SymbolSize = 16;
                btnColor.ColorTable = eButtonColor.Flat;
                itmpnlColor.Items.Add(btnColor);
            }
            SetCustomColor(SelectedColor);
        }

        /// <summary>
        /// 获取颜色的名称
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        private string GetName(Color color)
        {
            return "C" + color.R.ToString() + color.G.ToString() + color.B.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdColor_Executed(object sender, EventArgs e)
        {
            if (sender is ButtonX)
            {
                ButtonX btn = (ButtonX)sender;
                if (btn.Tag != null) _SelectedColor = (Color)btn.Tag;
            }
            else if (sender is ButtonItem)
            {
                ButtonItem btn = (ButtonItem)sender;
                if (btn.Tag != null) _SelectedColor = (Color)btn.Tag;
            }
            SelectChanged?.Invoke(this, new ColorEventArgs()
            {
                NewColor = SelectedColor
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="color"></param>
        private void SetSelected(Color color)
        {
            string name = GetName(color);
            int idx = itmpnlColor.Items.IndexOf(name);
            if (idx < 0) return;
            ((ButtonItem)itmpnlColor.Items[idx]).Checked = true;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkRGB_CheckedChanged(object sender, EventArgs e)
        {
            dynamic[] slds = new dynamic[] { sld1, sld2, sld3 };
            dynamic[] txts = new dynamic[] { txt1, txt2, txt3 };
            sld4.Visible = false;
            txt4.Visible = false;
            int max = 255;
            bool unit = false;
            int idx = 0;
            if (chkCMYK.Checked)
            {
                sld4.Visible = true;
                txt4.Visible = true;
                max = 100;
                unit = true;
                idx = 1;
            }
            foreach (dynamic itm in slds)
            {
                itm.Maximum = max;
                itm.Text = itm.CommandParameter.ToString().Split(',')[idx];
            }
            foreach (dynamic itm in txts)
            {
                itm.MaxValue = max;
                itm.ButtonCustom.Visible = unit;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sld_ValueChanged(object sender, EventArgs e)
        {
            dynamic[] txts = new dynamic[] { txt1, txt2, txt3 };
            Slider sld = (Slider)sender;
            if (sld.CommandParameter == null) txt4.Value = sld.Value;
            else
            {
                foreach (dynamic itm in txts)
                {
                    if (itm.CommandParameter != sld.CommandParameter) continue;
                    itm.Value = sld.Value;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_ValueChanged(object sender, EventArgs e)
        {
            dynamic[] slds = new dynamic[] { sld1, sld2, sld3 };
            IntegerInput txt = (IntegerInput)sender;
            if (txt.CommandParameter == null) sld4.Value = txt.Value;
            else
            {
                foreach (dynamic itm in slds)
                {
                    if (itm.CommandParameter != txt.CommandParameter) continue;
                    itm.Value = txt.Value;
                }
            }
            SetNewColor();
        }

        /// <summary>
        /// 
        /// </summary>
        private void SetNewColor()
        {
            if (chkCMYK.Checked)
            {
                btnCustom.SymbolColor = Convertor.IColor2Color(new CmykColor()
                {
                    Cyan = txt1.Value,
                    Magenta = txt2.Value,
                    Yellow = txt3.Value,
                    Black = txt4.Value
                });
            }
            else
            {
                btnCustom.SymbolColor = Convertor.IColor2Color(new RgbColor()
                {
                    Red = txt1.Value,
                    Green = txt2.Value,
                    Blue = txt3.Value
                });
            }
            btnCustom.Tag = btnCustom.SymbolColor;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clrBlender_SelectedColorChanged(object sender, EventArgs e)
        {
            SetCustomColor(clrBlender.SelectedColor);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="color"></param>
        private void SetCustomColor(Color color)
        {            
            if (chkRGB.Checked)
            {
                //IRgbColor clr = ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(color);
                txt1.Value = color.R;
                txt2.Value = color.G;
                txt3.Value = color.B;
            }
            else
            {
                double c = (double)(255 - color.R) / 255;
                double m = (double)(255 - color.G) / 255;
                double y = (double)(255 - color.B) / 255;
                double k = (double)Math.Min(c, Math.Min(m, y));
                if (k == 1.0)
                {
                    c = m = y = 0;
                }
                else
                {
                    c = (c - k) / (1 - k);
                    m = (m - k) / (1 - k);
                    y = (y - k) / (1 - k);
                }
                //ICmykColor clr = Convertor.Color2IColor(color) as ICmykColor;
                //if (clr == null) return;
                //txt1.Value = clr.Cyan;
                //txt2.Value = clr.Magenta;
                //txt3.Value = clr.Yellow;
                //txt4.Value = clr.Black;
                txt1.Value = (int)(c * 100);
                txt2.Value = (int)(m * 100);
                txt3.Value = (int)(y * 100);
                txt4.Value = (int)(k * 100);

            }
        }
        
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void ColorSelectedEvent(object sender, ColorEventArgs e);

    /// <summary>
    /// 
    /// </summary>
    public class ColorEventArgs : EventArgs
    {
        /// <summary>
        /// 
        /// </summary>
        public Color NewColor { get; set; }
    }

}
